﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WPFExcelReport.Helpers;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        public string AUFLFD(string ordType = "T6", string KEY = "ABKOPF",int increase=1)
        { 
            string OrdNum = "000000";
            OracleCommand or1;
            DataTable tbLocal;
            DataRow dwLocal = null;
            try
            {
                KEY = Left(KEY + "    ", 8);
                string SDKEY = "00" + KEY + ordType + "          00";

                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM SDDAT  WHERE SDSA='0022' AND SDKEY=:SDKEY";
                or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY; 
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    tbLocal.TableName = "SDDAT";
                    if (tbLocal.Rows.Count > 0)
                    {
                        dwLocal = tbLocal.Rows[0];
                    }
                }
                if (dwLocal == null)
                    return OrdNum;
                string Current = dwLocal["SDDATEN"].ToString().Substring(47, 6);
                long currentnum = System.Convert.ToInt64(Current);
                OrdNum = Right("000000" + (currentnum + increase).ToString(), 6);
                dwLocal["SDDATEN"] = dwLocal["SDDATEN"].ToString().Substring(0, 47) + OrdNum + dwLocal["SDDATEN"].ToString().Substring(53, 172 - 53);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "UPDATE SDDAT SET SDDATEN=:SDDATEN"
             + " WHERE SDSA='0022' AND SDKEY=:SDKEY";

                or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwLocal["SDDATEN"].ToString();
                or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
                setResult = Connect2Proxy(or1);

                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ABPAUFKRS,ABPAUFLFD,ABPAUFPOS FROM ABPOS WHERE ABPMANDANT=:ABPMANDANT AND ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD";
                or1.Parameters.Add("ABPMANDANT", OracleType.NVarChar).Value = "00";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = ordType;
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = OrdNum;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables[0].Rows.Count > 0)
                {
                    throw new Exception("生产单号码已经存在，若有异常，立即通知管理员");
                }
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); ;
            }
            return OrdNum;
        }
        public void CreateCPGB11(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            DataTable tbABP = db.Tables["ICC_GB1109"];
            double TARGETQTY = 0;
            double GLBSTKG = 0;
            double GLBSTNET = 0;
            double GLKOND = 0;
            double STDKOND = 0;
            string BEWEGUNG = "-1"; 
            double diff = 0;
            string BS = "42";
            int GABLFDNR = 1;
            DataRow dwGABEW = INTEXDBC.Convert2GABEW(null, app.company, false);
            DataRow dwGPBST = INTEXDBC.Convert2GPBST(null, app.company, false);
            DataRow dwGGBST = INTEXDBC.Convert2GGBST(null, app.company, false);
            DataRow dwZUTEI = INTEXDBC.Convert2ZUTEI(null, app.company, false);
            DataRow dwGAAUF = INTEXDBC.Convert2GAAUF(null, app.company, false);
            DataRow dwVKPOS = INTEXDBC.Convert2VKPOS(null, app.company, false);
            #region======Allocation========
            var qGZ = from DataRow row in tbABP.AsEnumerable()
                      where row["JP01"].ToString() == "N"
                      select row;
            BEWEGUNG = AUFLFD("  ", "BEWEGUNG", 2);
            foreach (DataRow dwGLLG in qGZ)
            {
                string VKPNUM = dwGLLG["JP04"].ToString();
                bool IsAllocated = false;
                if (VKPNUM.TrimEnd().Length > 10)
                {
                    IsAllocated = true; 
                }
                if (!IsAllocated)
                    continue;
                dwZUTEI["ZUMANDANT"] = dwGLLG["GLMANDANT"];
                dwZUTEI["ZULBEHI"] = dwGLLG["GLLBEHI"];
                SResult = INTEXDBC.QueryZUTEI(dwZUTEI, 12);
                bool IsZutei = false;
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    IsZutei = true; 
                    continue;
                }
                if (IsZutei)
                    continue;
             
                #region====ZUTEI======
                dwZUTEI["ZUMANDANT"] = app.Mandant;
                dwZUTEI["ZUAUFKRS"] = VKPNUM.Substring(0, 2);
                dwZUTEI["ZUAUFLFD"] = VKPNUM.Substring(2, 6);
                dwZUTEI["ZUAUFPOS"] = VKPNUM.Substring(8, 3);
                dwZUTEI["ZUSA"] = "610";
                dwZUTEI["ZUTEILNR"] = "0000";
                dwZUTEI["ZUPOS"] = "01";

                dwZUTEI["ZUPARTIE"] = dwGLLG["GLPARTIE"];
                dwZUTEI["ZUPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwZUTEI["ZULO"] = dwGLLG["GLLO"];
                dwZUTEI["ZULB"] = dwGLLG["GLLB"];
                dwZUTEI["ZULBEHI"] = dwGLLG["GLLBEHI"];
                dwZUTEI["ZUAENDZL"] = "0";
                dwZUTEI["ZUAENDBEN"] = "        ";
                dwZUTEI["ZUAENDDAT"] = "0";
                dwZUTEI["ZUNEUBEN"] = dwGLLG["CHGUSER"];
                dwZUTEI["ZUNEUDAT"] = dwGLLG["CHGDATE"];
                dwZUTEI["ZUEINTKG"] = dwGLLG["GLBSTKG"];
                dwZUTEI["ZUEINTSP"] = "0";
                dwZUTEI["ZUEINTKA"] = "0";
                dwZUTEI["ZUENTNKG"] = "0";
                dwZUTEI["ZUENTNSP"] = "0";
                dwZUTEI["ZUENTNKA"] = "0";
                dwZUTEI["ZULIEFZWP"] = "          ";
                dwZUTEI["ZUSPPART"] = dwGLLG["GPSPPART"];
                dwZUTEI["ZUART"] = dwGLLG["GPGART"];
                dwZUTEI["ZUVAR"] = dwGLLG["GPGVAR"];
                dwZUTEI["ZUFARBE"] = dwGLLG["GPGFARBE"];
                dwZUTEI["ZUDEZI"] = " ";
                dwZUTEI["ZUME"] = "1";
                dwZUTEI["ZUERLKZ"] = " ";
                dwZUTEI["ZUIKOND"] = "0";
                dwZUTEI["ZUFLAG"] = "P";
                dwZUTEI["ZUNTAUSCH"] = " ";
                dwZUTEI["ZUFILLER"] = "                          ";
                #endregion
                INTEXDBC.SaveZUTEI(dwZUTEI, app.company);
                #region======dwGABEW==================== 
                dwGABEW["GABMANDANT"] = app.Mandant;
                dwGABEW["GABJJJJ"] = dwGLLG["CHGDATE"].ToString().Substring(0, 4);
                dwGABEW["GABBELEG"] = BEWEGUNG;
                dwGABEW["GABLFDNR"] = GABLFDNR++;
                dwGABEW["GABPARTIE"] = dwGLLG["GLPARTIE"];
                dwGABEW["GABPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGABEW["GABSPSTA"] = "  ";
                dwGABEW["GABZENSUR"] = "1";
                dwGABEW["GABLO"] = dwGLLG["GLLO"];
                dwGABEW["GABLB"] = dwGLLG["GLLB"];
                dwGABEW["GABLP"] = dwGLLG["GLLP"];
                dwGABEW["GABAUFBKZ"] = " ";
                dwGABEW["GABLBEH"] = dwGLLG["GLLBEH"];
                dwGABEW["GABLBEHI"] = dwGLLG["GLLBEHI"];
                dwGABEW["GABART"] = dwGLLG["GPGART"];
                dwGABEW["GABVAR"] = dwGLLG["GPGVAR"];
                dwGABEW["GABFARBE"] = dwGLLG["GPGFARBE"];
                dwGABEW["GABDEZI"] = " ";
                dwGABEW["GABAUFTYP"] = "K";
                dwGABEW["GABAUFKRS"] =VKPNUM.Substring(0,2);
                dwGABEW["GABAUFLFD"] = VKPNUM.Substring(2, 6);
                dwGABEW["GABAUFPOS"] = VKPNUM.Substring(8, 3);
                dwGABEW["GABAFOLGE"] = "000";
                dwGABEW["GABTEILNR"] = "0000";
                dwGABEW["GABSA"] = "610";
                dwGABEW["GABPOS"] = "01";
                dwGABEW["GABKSTKRS"] = "  ";
                dwGABEW["GABKST"] = "0000";
                dwGABEW["GABKOSTART"] = "      ";
                dwGABEW["GABBS"] = "61";
                dwGABEW["GABWERTDAT"] = dwGLLG["CHGDATE"];
                dwGABEW["GABKG"] = dwGLLG["GLBSTKG"];
                dwGABEW["GABKA"] = dwGLLG["GLBSTKA"];
                dwGABEW["GABSP"] = dwGLLG["GLBSTSP"];
                dwGABEW["GABWERT"] = "0";
                dwGABEW["GABWEKORR"] = "  ";
                dwGABEW["GABBUCHDAT"] = dwGLLG["CHGDATE"];
                dwGABEW["GABBUCHZEIT"] = dwGLLG["CHGTIME"];
                dwGABEW["GABBUCHBEN"] = dwGLLG["CHGUSER"];
                dwGABEW["GABFC"] = "GZ18    ";
                dwGABEW["GABKONDAT"] = "0";
                dwGABEW["GABMATABR"] = " ";
                dwGABEW["GABSTORKZ"] = " ";
                dwGABEW["GABINVBLG"] = "0";
                dwGABEW["GABDISPOKZ"] = " ";
                dwGABEW["GABAPOS"] = "0000";
                dwGABEW["GABACTION"] = "  ";
                dwGABEW["GABIKOND"] = "0000";
                dwGABEW["GABCARRIER"] = "        ";
                dwGABEW["GABCARRPOS"] = "000";
                dwGABEW["GABMNRKRS"] = "  ";
                dwGABEW["GABMLFDNR"] = "0000";
                dwGABEW["GABMPOS"] = "00";
                dwGABEW["GABSCHICHT"] = "0";
                dwGABEW["GABFILLER"] = "                                       ";
                #endregion
                dwGAAUF["GAMANDANT"] = dwZUTEI["ZUMANDANT"];
                dwGAAUF["GAAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                dwGAAUF["GAAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                dwGAAUF["GAAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                INTEXDBC.UpdateGAAUF(dwGAAUF, 11);

                dwVKPOS["VKPMANDANT"] = dwZUTEI["ZUMANDANT"];
                dwVKPOS["VKPAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                dwVKPOS["VKPAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                dwVKPOS["VKPAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                INTEXDBC.UpdateVKPOS(dwVKPOS, app.company, 7);
                INTEXDBC.SaveGABEW(dwGABEW, app.company);
                dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGPBST["GPBLO"] = dwGLLG["GLLO"];
                dwGPBST["GPBLB"] = dwGLLG["GLLB"];
                INTEXDBC.UpdateGPBST(dwGPBST, 3);  
            }
            #endregion
            #region======qGB11========
            var qGB11 = from DataRow row in tbABP.AsEnumerable()
                        //where row["JP01"].ToString() == "N"
                        select row;
            BEWEGUNG = AUFLFD("  ", "BEWEGUNG", 2);
            GABLFDNR=1;
            foreach (DataRow dwGLLG in qGB11)
            {
                //根据此时的情况，判断ABFOLGE

                /*修改/插入 GPBST，GGBST GABEW 
                 * GLBSTNET=X,
                 * GLBSTKG=Y
                 * 毛重=（净重+毛皮）不变，净重修正=目标重量，交易重量跟着变（回潮计算）
                    GB14是“交易重量跟着变化的重量”
                 */

                TARGETQTY = Convert.ToDouble(dwGLLG["TARGETQTY"].ToString());
                GLBSTNET = Convert.ToDouble(dwGLLG["GLBSTNET"].ToString());
                GLBSTKG = Convert.ToDouble(dwGLLG["GLBSTKG"].ToString());//17500

                diff = TARGETQTY - GLBSTKG;
                //if (diff == 0)
                //    continue;
                //if (TARGETQTY == 0)
                //    continue;
                /* Y=GLBSTKG=17570  X=GLBSTNET=17000 STDKOND=850 GLKOND=500
                 X/1.05=Y/1.085
                 Y=(X*1.085)/1.05
                 * =17570*1.05/17000
                 * */

                GLKOND = Convert.ToDouble(dwGLLG["GLKOND"].ToString()) / 10000;//500
                STDKOND = (GLBSTKG * (1 + GLKOND) / GLBSTNET) - 1;//850
                GLBSTKG = TARGETQTY;
                GLBSTNET = Helper.RoundX(GLBSTKG * (1 + GLKOND) / (1 + STDKOND) * 0.001, 100) * 1000;
                dwGLLG["GLBSTNET"] = GLBSTNET;
                dwGLLG["GLBSTKG"] = GLBSTKG;

                INTEXDBC.UpdateGLLG(dwGLLG, 2);
                string jp01 = dwGLLG["JP01"].ToString().Trim();
                if(jp01 == "N")
                {
                    BS = "42";

                    if (diff > 0)
                    {
                        BS = "43";
                    }

                    #region======dwGABEW====================
                    dwGABEW["GABMANDANT"] = app.Mandant;
                    dwGABEW["GABJJJJ"] = dwGLLG["CHGDATE"].ToString().Substring(0, 4);
                    dwGABEW["GABBELEG"] = BEWEGUNG;
                    dwGABEW["GABLFDNR"] = GABLFDNR++; 
                    dwGABEW["GABPARTIE"] = dwGLLG["GLPARTIE"];
                    dwGABEW["GABPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                    dwGABEW["GABSPSTA"] = "  ";
                    dwGABEW["GABZENSUR"] = "1";
                    dwGABEW["GABLO"] = dwGLLG["GLLO"];
                    dwGABEW["GABLB"] = dwGLLG["GLLB"];
                    dwGABEW["GABLP"] = dwGLLG["GLLP"];
                    dwGABEW["GABAUFBKZ"] = " ";
                    dwGABEW["GABLBEH"] = dwGLLG["GLLBEH"];
                    dwGABEW["GABLBEHI"] = dwGLLG["GLLBEHI"];
                    dwGABEW["GABART"] = dwGLLG["GPGART"];
                    dwGABEW["GABVAR"] = dwGLLG["GPGVAR"];
                    dwGABEW["GABFARBE"] = dwGLLG["GPGFARBE"];
                    dwGABEW["GABDEZI"] = " ";
                    dwGABEW["GABAUFTYP"] = " ";
                    dwGABEW["GABAUFKRS"] = "  ";
                    dwGABEW["GABAUFLFD"] = "000000";
                    dwGABEW["GABAUFPOS"] = "000";
                    dwGABEW["GABAFOLGE"] = "000";
                    dwGABEW["GABTEILNR"] = "0000";
                    dwGABEW["GABSA"] = "   ";
                    dwGABEW["GABPOS"] = "00";
                    dwGABEW["GABKSTKRS"] = "  ";
                    dwGABEW["GABKST"] = "0000";
                    dwGABEW["GABKOSTART"] = "      ";
                    dwGABEW["GABBS"] = BS;
                    dwGABEW["GABWERTDAT"] = dwGLLG["CHGDATE"];
                    dwGABEW["GABKG"] = diff;
                    dwGABEW["GABKA"] = "0";
                    dwGABEW["GABSP"] = "0";
                    dwGABEW["GABWERT"] = "0";
                    dwGABEW["GABWEKORR"] = "  ";
                    dwGABEW["GABBUCHDAT"] = dwGLLG["CHGDATE"];
                    dwGABEW["GABBUCHZEIT"] = dwGLLG["CHGTIME"];
                    dwGABEW["GABBUCHBEN"] = dwGLLG["CHGUSER"];
                    dwGABEW["GABFC"] = "GB11    ";
                    dwGABEW["GABKONDAT"] = "0";
                    dwGABEW["GABMATABR"] = " ";
                    dwGABEW["GABSTORKZ"] = " ";
                    dwGABEW["GABINVBLG"] = "0";
                    dwGABEW["GABDISPOKZ"] = " ";
                    dwGABEW["GABAPOS"] = "0000";
                    dwGABEW["GABACTION"] = "  ";
                    dwGABEW["GABIKOND"] = "0000";
                    dwGABEW["GABCARRIER"] = "        ";
                    dwGABEW["GABCARRPOS"] = "000";
                    dwGABEW["GABMNRKRS"] = "  ";
                    dwGABEW["GABMLFDNR"] = "0000";
                    dwGABEW["GABMPOS"] = "00";
                    dwGABEW["GABSCHICHT"] = "0";
                    dwGABEW["GABFILLER"] = "                                       ";
                    #endregion
                    INTEXDBC.SaveGABEW(dwGABEW, app.company);

                    #region=======dwGPBST==========

                    dwGPBST["GPBMANDANT"] = app.Mandant;
                    dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                    dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                    dwGPBST["GPBLO"] = dwGLLG["GLLO"];
                    dwGPBST["GPBLB"] = dwGLLG["GLLB"];
                    dwGPBST["GPBAENDZL"] = "0";
                    dwGPBST["GPBAENDBEN"] = "        ";
                    dwGPBST["GPBAENDDAT"] = "0";
                    dwGPBST["GPBNEUBEN"] = dwGLLG["CHGUSER"];
                    dwGPBST["GPBNEUDAT"] = dwGLLG["CHGDATE"];
                    dwGPBST["GPBART"] = dwGLLG["GPGART"];
                    dwGPBST["GPBVAR"] = dwGLLG["GPGVAR"];
                    dwGPBST["GPBFARBE"] = dwGLLG["GPGFARBE"];
                    dwGPBST["GPBDEZI"] = " ";
                    dwGPBST["GPBBSTKGG"] = dwGLLG["GLBSTKG"];
                    dwGPBST["GPBBSTKAG"] = "0";
                    dwGPBST["GPBBSTSPG"] = "0";
                    dwGPBST["GPBBSTKGZ"] = "0";
                    dwGPBST["GPBBSTKAZ"] = "0";
                    dwGPBST["GPBBSTSPZ"] = "0";
                    dwGPBST["GPBEINDAT"] = dwGLLG["CHGDATE"];
                    dwGPBST["GPBSPPART"] = dwGLLG["GPSPPART"];
                    dwGPBST["GPBZWPART"] = dwGLLG["GPZWPART"];
                    dwGPBST["GPBFILLER"] = "                            ";
                    INTEXDBC.SaveGPBST(dwGPBST, app.company);
                    #endregion

                    #region=======dwGGBST==============

                    dwGGBST["GGMANDANT"] = app.Mandant;
                    dwGGBST["GGSA"] = "00";
                    dwGGBST["GGART"] = dwGLLG["GPGART"];
                    dwGGBST["GGVAR"] = dwGLLG["GPGVAR"];
                    dwGGBST["GGFARBE"] = dwGLLG["GPGFARBE"];
                    dwGGBST["GGZENSUR"] = "1";
                    dwGGBST["GGLO"] = dwGLLG["GLLO"];
                    dwGGBST["GGMJJ"] = dwGLLG["CHGDATE"].ToString().Substring(0, 4);
                    dwGGBST["GGMM"] = dwGLLG["CHGDATE"].ToString().Substring(4, 2);
                    dwGGBST["GGAKTKZ"] = "A";
                    dwGGBST["GGME01"] = "0";
                    dwGGBST["GGME10"] = "0";
                    dwGGBST["GGME11"] = "0";
                    dwGGBST["GGME12"] = "0";
                    dwGGBST["GGME13"] = "0";
                    dwGGBST["GGME20"] = "0";
                    dwGGBST["GGME21"] = "0";
                    dwGGBST["GGME26"] = "0";
                    dwGGBST["GGME27"] = "0";
                    dwGGBST["GGME30"] = "0";
                    dwGGBST["GGME31"] = "0";
                    dwGGBST["GGME32"] = "0";
                    dwGGBST["GGME33"] = "0";
                    dwGGBST["GGME34"] = "0";
                    dwGGBST["GGME35"] = "0";
                    dwGGBST["GGME36"] = "0";
                    dwGGBST["GGME37"] = "0";
                    dwGGBST["GGME38"] = "0";
                    dwGGBST["GGME40"] = "0";
                    dwGGBST["GGME41"] = "0";
                    if (BS == "42")
                    {
                        dwGGBST["GGME42"] = diff;
                    }
                    else
                    {
                        dwGGBST["GGME43"] = diff;
                    }
                    dwGGBST["GGME46"] = "0";
                    dwGGBST["GGME47"] = "0";
                    dwGGBST["GGME50"] = "0";
                    dwGGBST["GGME51"] = "0";
                    dwGGBST["GGME52"] = "0";
                    dwGGBST["GGME53"] = "0";
                    dwGGBST["GGME55"] = "0";
                    dwGGBST["GGME56"] = "0";
                    dwGGBST["GGME57"] = "0";
                    dwGGBST["GGME58"] = "0";
                    dwGGBST["GGME73"] = "0";
                    dwGGBST["GGME76"] = "0";
                    dwGGBST["GGME77"] = "0";
                    dwGGBST["GGME78"] = "0";
                    dwGGBST["GGME80"] = "0";
                    dwGGBST["GGME88"] = "0";
                    dwGGBST["GGME89"] = "0";
                    dwGGBST["GGME90"] = "0";
                    dwGGBST["GGME91"] = "0";
                    dwGGBST["GGME92"] = "0";
                    dwGGBST["GGME93"] = "0";
                    dwGGBST["GGMER03"] = "0";
                    dwGGBST["GGMER04"] = "0";
                    dwGGBST["GGMER05"] = "0";
                    dwGGBST["GGMER06"] = "0";
                    dwGGBST["GGMER07"] = "0";
                    dwGGBST["GGBSTA"] = dwGLLG["GLBSTKG"];
                    dwGGBST["GGBSTAN"] = "0";
                    dwGGBST["GGBSTKEN"] = "0";
                    dwGGBST["GGBSTKZU"] = "0";
                    dwGGBST["GGBSTKMA"] = "0";
                    dwGGBST["GGBSTKME"] = "0";
                    dwGGBST["GGBSTMA"] = "0";
                    dwGGBST["GGBSTME"] = dwGLLG["GLBSTKG"];
                    dwGGBST["GGBSTVMA"] = "0";
                    dwGGBST["GGBSTVME"] = dwGLLG["GLBSTKG"];
                    dwGGBST["GGMEIV"] = "0";
                    dwGGBST["GGINDAT"] = "0";
                    dwGGBST["GGINBST"] = "0";
                    dwGGBST["GGINALT"] = "0";
                    dwGGBST["GGEKPRMA"] = "0";
                    dwGGBST["GGEKPRME"] = "0";
                    dwGGBST["GGEKPRZU"] = "0";
                    dwGGBST["GGVRPR"] = "0";
                    dwGGBST["GGDLABST"] = "426";
                    dwGGBST["GGDLATAG"] = "21";
                    dwGGBST["GGMINBST"] = dwGLLG["GLBSTKG"];
                    dwGGBST["GGMAXBST"] = dwGLLG["GLBSTKG"];
                    dwGGBST["GGFERTKZ"] = "E";
                    dwGGBST["GGUEBER"] = "N";
                    dwGGBST["GGFILLER1"] = "                                                   ";
                    dwGGBST["GGAENDZL"] = "0";
                    dwGGBST["GGAENDBEN"] = "        ";
                    dwGGBST["GGAENDDAT"] = "0";
                    dwGGBST["GGNEUBEN"] = dwGLLG["CHGUSER"];
                    dwGGBST["GGNEUDAT"] = dwGLLG["CHGDATE"];
                    INTEXDBC.SaveGGBST(dwGGBST, app.company);
                    #endregion
                    EventDispatcher.Instance.RaiseStatus("YGRCPGB04转换目标重量完成:" + dwGLLG["GLLBEHI"].ToString(), StatusEventType.StopProgress);
                }
            }
            #endregion
            #region===qGB09=======
            var qGB09 = from DataRow row in tbABP.AsEnumerable()
                        where row["JP02"].ToString() == "N"
                        select row;
            BEWEGUNG = AUFLFD("  ", "BEWEGUNG", 2);
            GABLFDNR = 1;
            foreach (DataRow dwGLLG in qGB09)
            {
              
                #region======dwGABEW====================
                dwGABEW["GABMANDANT"] = app.Mandant;
                dwGABEW["GABJJJJ"] = dwGLLG["NEWDATE"].ToString().Substring(0, 4);
                dwGABEW["GABBELEG"] = BEWEGUNG;
                dwGABEW["GABLFDNR"] = GABLFDNR++;
                dwGABEW["GABPARTIE"] = dwGLLG["GLPARTIE"];
                dwGABEW["GABPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGABEW["GABSPSTA"] = "  ";
                dwGABEW["GABZENSUR"] = "1";
                dwGABEW["GABLO"] = dwGLLG["GLLO"];
                dwGABEW["GABLB"] = dwGLLG["GLLB"];
                dwGABEW["GABLP"] = dwGLLG["GLLP"];
                dwGABEW["GABAUFBKZ"] = "N";
                dwGABEW["GABLBEH"] = dwGLLG["GLLBEH"];
                dwGABEW["GABLBEHI"] = dwGLLG["GLLBEHI"];
                dwGABEW["GABART"] = dwGLLG["GPGART"];
                dwGABEW["GABVAR"] = dwGLLG["GPGVAR"];
                dwGABEW["GABFARBE"] = dwGLLG["GPGFARBE"];
                dwGABEW["GABDEZI"] = " ";
                dwGABEW["GABAUFTYP"] = " ";
                dwGABEW["GABAUFKRS"] = "  ";
                dwGABEW["GABAUFLFD"] = "000000";
                dwGABEW["GABAUFPOS"] = "000";
                dwGABEW["GABAFOLGE"] = "000";
                dwGABEW["GABTEILNR"] = "0000";
                dwGABEW["GABSA"] = "   ";
                dwGABEW["GABPOS"] = "00";
                dwGABEW["GABKSTKRS"] = "  ";
                dwGABEW["GABKST"] = "0000";
                dwGABEW["GABKOSTART"] = "      ";
                dwGABEW["GABBS"] = "20";
                dwGABEW["GABWERTDAT"] = dwGLLG["NEWDATE"];
                dwGABEW["GABKG"] = dwGLLG["GLBSTKG"];
                dwGABEW["GABKA"] = dwGLLG["GLBSTKA"];
                dwGABEW["GABSP"] = dwGLLG["GLBSTSP"];
                dwGABEW["GABWERT"] = "0";
                dwGABEW["GABWEKORR"] = "  ";
                dwGABEW["GABBUCHDAT"] = dwGLLG["NEWDATE"];
                dwGABEW["GABBUCHZEIT"] = dwGLLG["NEWTIME"];
                dwGABEW["GABBUCHBEN"] = dwGLLG["NEWUSER"];
                dwGABEW["GABFC"] = "GB09    ";
                dwGABEW["GABKONDAT"] = "0";
                dwGABEW["GABMATABR"] = " ";
                dwGABEW["GABSTORKZ"] = " ";
                dwGABEW["GABINVBLG"] = "0";
                dwGABEW["GABDISPOKZ"] = " ";
                dwGABEW["GABAPOS"] = "0000";
                dwGABEW["GABACTION"] = "  ";
                dwGABEW["GABIKOND"] = "0000";
                dwGABEW["GABCARRIER"] = "        ";
                dwGABEW["GABCARRPOS"] = "000";
                dwGABEW["GABMNRKRS"] = "  ";
                dwGABEW["GABMLFDNR"] = "0000";
                dwGABEW["GABMPOS"] = "00";
                dwGABEW["GABSCHICHT"] = "0";
                dwGABEW["GABFILLER"] = " " + dwGLLG["CPLO"].ToString() + dwGLLG["CPLB"].ToString() + dwGLLG["CPLP"].ToString() + "N                         ";
                #endregion
                INTEXDBC.SaveGABEW(dwGABEW);
                dwGABEW["GABBELEG"] = BEWEGUNG;
                dwGABEW["GABLFDNR"] = GABLFDNR++;
                dwGABEW["GABLO"] = dwGLLG["CPLO"];
                dwGABEW["GABLB"] = dwGLLG["CPLB"];
                dwGABEW["GABLP"] = dwGLLG["CPLP"]; 
                dwGABEW["GABBS"] = "21";
                dwGABEW["GABFILLER"] = " " + dwGLLG["GLLO"].ToString() + dwGLLG["GLLB"].ToString() + dwGLLG["GLLP"].ToString() + "N                         ";// " 01CPA       N                         ";
                INTEXDBC.SaveGABEW(dwGABEW);
                INTEXDBC.UpdateGLLG(dwGLLG, 3);//移入新仓库CP
                #region=======dwGPBST==========

                dwGPBST["GPBMANDANT"] = app.Mandant;
                dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGPBST["GPBLO"] = dwGLLG["CPLO"];
                dwGPBST["GPBLB"] = dwGLLG["CPLB"];
                dwGPBST["GPBAENDZL"] = "0";
                dwGPBST["GPBAENDBEN"] = "        ";
                dwGPBST["GPBAENDDAT"] = "0";
                dwGPBST["GPBNEUBEN"] = dwGLLG["NEWUSER"];
                dwGPBST["GPBNEUDAT"] = dwGLLG["NEWDATE"];
                dwGPBST["GPBART"] = dwGLLG["GPGART"];
                dwGPBST["GPBVAR"] = dwGLLG["GPGVAR"];
                dwGPBST["GPBFARBE"] = dwGLLG["GPGFARBE"];
                dwGPBST["GPBDEZI"] = " ";
                dwGPBST["GPBBSTKGG"] = dwGLLG["GLBSTKG"];
                dwGPBST["GPBBSTKAG"] = "0";
                dwGPBST["GPBBSTSPG"] = "0";
                dwGPBST["GPBBSTKGZ"] = "0";
                dwGPBST["GPBBSTKAZ"] = "0";
                dwGPBST["GPBBSTSPZ"] = "0";
                dwGPBST["GPBEINDAT"] = dwGLLG["NEWDATE"];
                dwGPBST["GPBSPPART"] = dwGLLG["GPSPPART"];
                dwGPBST["GPBZWPART"] = dwGLLG["GPZWPART"];
                dwGPBST["GPBFILLER"] = "                            ";
                INTEXDBC.SaveGPBST(dwGPBST, app.company);//这里应当执行更新新库区信息，没有就新建，如有则更新
                
                #endregion
                #region=======dwGGBST==============
                dwGGBST["GGMANDANT"] = app.Mandant;
                dwGGBST["GGSA"] = "00";
                dwGGBST["GGART"] = dwGLLG["GPGART"];
                dwGGBST["GGVAR"] = dwGLLG["GPGVAR"];
                dwGGBST["GGFARBE"] = dwGLLG["GPGFARBE"];
                dwGGBST["GGZENSUR"] = "1";
                dwGGBST["GGLO"] = dwGLLG["GLLO"];
                dwGGBST["GGMJJ"] = dwGLLG["NEWDATE"].ToString().Substring(0, 4);
                dwGGBST["GGMM"] = dwGLLG["NEWDATE"].ToString().Substring(4, 2);
                dwGGBST["GGAKTKZ"] = "A";
                dwGGBST["GGME01"] = "0";
                dwGGBST["GGME10"] = "0";
                dwGGBST["GGME11"] = "0";
                dwGGBST["GGME12"] = "0";
                dwGGBST["GGME13"] = "0";
                dwGGBST["GGME20"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGME21"] = "0";
                dwGGBST["GGME26"] = "0";
                dwGGBST["GGME27"] = "0";
                dwGGBST["GGME30"] = "0";
                dwGGBST["GGME31"] = "0";
                dwGGBST["GGME32"] = "0";
                dwGGBST["GGME33"] = "0";
                dwGGBST["GGME34"] = "0";
                dwGGBST["GGME35"] = "0";
                dwGGBST["GGME36"] = "0";
                dwGGBST["GGME37"] = "0";
                dwGGBST["GGME38"] = "0";
                dwGGBST["GGME40"] = "0";
                dwGGBST["GGME41"] = "0";
                dwGGBST["GGME42"] = "0";
                dwGGBST["GGME43"] = "0";
                dwGGBST["GGME46"] = "0";
                dwGGBST["GGME47"] = "0";
                dwGGBST["GGME50"] = "0";
                dwGGBST["GGME51"] = "0";
                dwGGBST["GGME52"] = "0";
                dwGGBST["GGME53"] = "0";
                dwGGBST["GGME55"] = "0";
                dwGGBST["GGME56"] = "0";
                dwGGBST["GGME57"] = "0";
                dwGGBST["GGME58"] = "0";
                dwGGBST["GGME73"] = "0";
                dwGGBST["GGME76"] = "0";
                dwGGBST["GGME77"] = "0";
                dwGGBST["GGME78"] = "0";
                dwGGBST["GGME80"] = "0";
                dwGGBST["GGME88"] = "0";
                dwGGBST["GGME89"] = "0";
                dwGGBST["GGME90"] = "0";
                dwGGBST["GGME91"] = "0";
                dwGGBST["GGME92"] = "0";
                dwGGBST["GGME93"] = "0";
                dwGGBST["GGMER03"] = "0";
                dwGGBST["GGMER04"] = "0";
                dwGGBST["GGMER05"] = "0";
                dwGGBST["GGMER06"] = "0";
                dwGGBST["GGMER07"] = "0";
                dwGGBST["GGBSTA"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGBSTAN"] = "0";
                dwGGBST["GGBSTKEN"] = "0";
                dwGGBST["GGBSTKZU"] = "0";
                dwGGBST["GGBSTKMA"] = "0";
                dwGGBST["GGBSTKME"] = "0";
                dwGGBST["GGBSTMA"] = "0";
                dwGGBST["GGBSTME"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGBSTVMA"] = "0";
                dwGGBST["GGBSTVME"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGMEIV"] = "0";
                dwGGBST["GGINDAT"] = "0";
                dwGGBST["GGINBST"] = "0";
                dwGGBST["GGINALT"] = "0";
                dwGGBST["GGEKPRMA"] = "0";
                dwGGBST["GGEKPRME"] = "0";
                dwGGBST["GGEKPRZU"] = "0";
                dwGGBST["GGVRPR"] = "0";
                dwGGBST["GGDLABST"] = "426";
                dwGGBST["GGDLATAG"] = "21";
                dwGGBST["GGMINBST"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGMAXBST"] = dwGLLG["GLBSTKG"];
                dwGGBST["GGFERTKZ"] = "E";
                dwGGBST["GGUEBER"] = "N";
                dwGGBST["GGFILLER1"] = "                                                   ";
                dwGGBST["GGAENDZL"] = "0";
                dwGGBST["GGAENDBEN"] = "        ";
                dwGGBST["GGAENDDAT"] = "0";
                dwGGBST["GGNEUBEN"] = dwGLLG["NEWUSER"];
                dwGGBST["GGNEUDAT"] = dwGLLG["NEWDATE"];
                INTEXDBC.SaveGGBST(dwGGBST, app.company);
                dwGGBST["GGLO"] = dwGLLG["CPLO"];
                dwGGBST["GGME20"] = "0";
                dwGGBST["GGME21"] = dwGLLG["GLBSTKG"];
                INTEXDBC.SaveGGBST(dwGGBST, app.company);
                EventDispatcher.Instance.RaiseStatus("YGRCPGB04入库:" + dwGLLG["GLLBEHI"].ToString(), StatusEventType.StopProgress);
                #endregion

               
                dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGPBST["GPBLO"] = dwGLLG["GLLO"];
                dwGPBST["GPBLB"] = dwGLLG["GLLB"];
                INTEXDBC.UpdateGPBST(dwGPBST, 3);//处理旧仓库，若0数量，删除
                INTEXDBC.DeleteGPBST(dwGPBST, 2);

            }
            #endregion
            #region===GPDAT=====
            var qGPDAT = from DataRow row in tbABP.AsEnumerable()
                        //where row["JP02"].ToString() == "N"
                        select row;
            foreach (DataRow dwGLLG in qGPDAT)
            {                
                dwZUTEI["ZUMANDANT"] = dwGLLG["GLMANDANT"];
                dwZUTEI["ZUPARTIE"] = dwGLLG["GLPARTIE"];
                dwZUTEI["ZUPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwZUTEI["ZUSPPART"] = dwGLLG["GPFBPART"];
                INTEXDBC.UpdateGPDAT(dwZUTEI, 1);
            }
            #endregion
            #region===GZ18=======
            var qGZ18 = from DataRow row in tbABP.AsEnumerable()
                        where row["JP02"].ToString() == "N"
                        select row;
            BEWEGUNG = AUFLFD("  ", "BEWEGUNG", 2);
            GABLFDNR = 1;
            foreach (DataRow dwGLLG in qGZ18)
            {

                dwZUTEI["ZUMANDANT"] = dwGLLG["GLMANDANT"];
                dwZUTEI["ZULBEHI"] = dwGLLG["GLLBEHI"];
                SResult = INTEXDBC.QueryZUTEI(dwZUTEI, 12);
                bool IsAllocated = false;
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    IsAllocated = true;
                    dwZUTEI = row;
                    continue;
                }

                if (!IsAllocated)
                    continue;
                //已经存在留位
                dwZUTEI["ZULO"] = dwGLLG["CPLO"];
                dwZUTEI["ZULB"] = dwGLLG["CPLB"];

                dwZUTEI["ZUEINTKG"] = dwGLLG["GLBSTKG"];
                dwZUTEI["ZUFLAG"] = "S";
                INTEXDBC.UpdateZUTEI(dwZUTEI, 2);

                dwGAAUF["GAMANDANT"] = dwZUTEI["ZUMANDANT"];
                dwGAAUF["GAAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                dwGAAUF["GAAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                dwGAAUF["GAAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                INTEXDBC.UpdateGAAUF(dwGAAUF, 11);

                dwVKPOS["VKPMANDANT"] = dwZUTEI["ZUMANDANT"];
                dwVKPOS["VKPAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                dwVKPOS["VKPAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                dwVKPOS["VKPAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                INTEXDBC.UpdateVKPOS(dwVKPOS, app.company, 7);

                dwGABEW["GABMANDANT"] = dwZUTEI["ZUMANDANT"];
                dwGABEW["GABAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                dwGABEW["GABAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                dwGABEW["GABAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                dwGABEW["GABLO"] = dwGLLG["GLLO"];
                dwGABEW["GABLB"] = dwGLLG["GLLB"];
                dwGABEW["GABLP"] = dwGLLG["GLLP"];
                dwGABEW["GABLBEHI"] = dwZUTEI["ZULBEHI"];
                dwGABEW["GABBS"] = "61";              
                
                dwGABEW["GABBELEG"] = BEWEGUNG;
                dwGABEW["GABLFDNR"] = GABLFDNR++;
                dwGABEW["GABBS"] = "62";
                dwGABEW["GABWERTDAT"] = dwGLLG["CHGDATE"];
                dwGABEW["GABBUCHDAT"] = dwGLLG["NEWDATE"];
                dwGABEW["GABBUCHZEIT"] = dwGLLG["NEWTIME"];
                dwGABEW["GABBUCHBEN"] = dwGLLG["NEWUSER"];
                INTEXDBC.SaveGABEW(dwGABEW, app.company);
                 
                dwGABEW["GABBELEG"] = BEWEGUNG;
                dwGABEW["GABLFDNR"] = GABLFDNR++;
                dwGABEW["GABBS"] = "61";

                dwGABEW["GABLO"] = dwGLLG["CPLO"];
                dwGABEW["GABLB"] = dwGLLG["CPLB"];
                dwGABEW["GABLP"] = dwGLLG["CPLP"];
                dwGABEW["GABKG"] = dwGLLG["GLBSTKG"];
                dwGABEW["GABWERTDAT"] = dwGLLG["CHGDATE"];
                dwGABEW["GABBUCHDAT"] = dwGLLG["NEWDATE"];
                dwGABEW["GABBUCHZEIT"] = dwGLLG["NEWTIME"];
                dwGABEW["GABBUCHBEN"] = dwGLLG["NEWUSER"];
                INTEXDBC.SaveGABEW(dwGABEW, app.company);

                dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGPBST["GPBLO"] = dwGLLG["GLLO"];
                dwGPBST["GPBLB"] = dwGLLG["GLLB"];
                INTEXDBC.UpdateGPBST(dwGPBST, 3);
                INTEXDBC.DeleteGPBST(dwGPBST, 2);
                dwGPBST["GPBPARTIE"] = dwGLLG["GLPARTIE"];
                dwGPBST["GPBPARTIEKZ"] = dwGLLG["GLPARTIEKZ"];
                dwGPBST["GPBLO"] = dwGLLG["CPLO"];
                dwGPBST["GPBLB"] = dwGLLG["CPLB"];
                INTEXDBC.UpdateGPBST(dwGPBST, 3);
                INTEXDBC.DeleteGPBST(dwGPBST, 2);
            }
            #endregion
            foreach (DataRow dwGLLG in tbABP.Rows)
            {
                dwGLLG["PROCESSED"] = "Y";
                dwGLLG["JP01"] = "Y";
                dwGLLG["JP02"] = "Y";
                INTEXDBC.SaveICC_GB1109(dwGLLG, app.company);
            }
        }
        void DoYGRCPGB04Work()
        {
            if (IsRun)
            {
                return;
            }
            IsRun = true;
            string path = "C:/IntexReport/Data/YGRCPGB04";
            YGRCPGB04(app.company, path);
        }
        public DataSet GetYGRCPGB04Job(string path)
        {
            DataTable SResult;
            DataSet SReturn = new DataSet();

            try
            {
                SQLiteDatabase SQLiteDB;
                string dbConnection = path + "/YGRCPGB04.s3db";
                SQLiteDB = new SQLiteDatabase(dbConnection);

                string sql = "SELECT* FROM ICC_GB1109 WHERE JP01='N' OR JP02='N' ORDER BY ABAID";
                SResult = SQLiteDB.GetDataTable(sql);
                SReturn.Merge(SResult);
                foreach (DataRow row in SResult.Rows)
                {
                    sql = "UPDATE ICC_GB1109 SET PROCESSED='Y', JP01='Y',JP02='Y' WHERE ABAID='" + row["ABAID"].ToString() + "'";
                    SQLiteDB.ExecuteNonQuery(sql);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void YGRCPGB04(string ITXDBVERSION, string path)
        {
            try
            {
                EventDispatcher.Instance.RaiseStatus("YGRCPGB04启动", StatusEventType.StopProgress);
                while (true)
                {
                    DataSet SetJob = GetYGRCPGB04Job(path);
                    if (SetJob.Tables[0].Rows.Count == 0)
                    {
                        IsRun = false;
                        break;
                    }
                    CreateCPGB11(ITXDBVERSION, SetJob);

                }
                EventDispatcher.Instance.RaiseStatus("YGRCPGB04完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                EventDispatcher.Instance.RaiseStatus("YGRCPGB04异常终止", StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsRun = false;
            }
        }
    }
}

